﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.Alexander.EDAS;
using System.Net;

namespace EDAS_CLient
{
    class Program
    {
        static void Main(string[] args)
        {

            string Data = "Hello i am a string\r\n";
            string ip = "192.168.87.53";

            AsyncSocket sock = null;
            for (int i = 0; i < 1000; i++)
            {
                sock = new AsyncSocket(new IPEndPoint(IPAddress.Parse("192.168.87.53"), 0), 1024);
                sock.OnError += new Action<Exception, string>(sock_OnError);
                sock.Connected = x =>
                {
                    x.Disconnected += new Action<long>(x_Disconnected);
                    Console.WriteLine("Connected");
                    TokenProcessor proc = new TokenProcessor(0x0A);
                    proc.OnToken += new Action<IWorkingSocket, byte[]>(proc_OnLine);
                    x.Processor = proc;
                    x.SendWaitReceive(Encoding.UTF8.GetBytes(Data));
                };
                sock.ID = i;
                sock.ConnectTo(new IPEndPoint(IPAddress.Parse(ip), 8888));
            }

            Console.ReadKey();
        }

        static void x_Disconnected(long obj)
        {
            //throw new NotImplementedException();
            Console.WriteLine(obj + " is disconnected");
        }

        static void proc_OnLine(IWorkingSocket arg1, byte[] arg2)
        {
            //throw new NotImplementedException();

            arg1.SendToQueue(arg2);
            long rc = arg1.SessionData != null ? (long)arg1.SessionData : 0;
            rc = rc + 1;
            arg1.SessionData = rc;
            if (rc % 100 == 0)
            {
                Console.WriteLine(arg1.ID + " is working string :" + Encoding.UTF8.GetString(arg2));
            }
        }

        static void sock_OnError(Exception arg1, string arg2)
        {
            //throw new NotImplementedException();
            Console.WriteLine(arg2);
            if (arg1 != null)
            {
                Console.WriteLine(arg1.Message);
            }
        }
    }


}
